package com.wyt.controller.purchaseController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.wyt.dao.*;
import com.wyt.entity.*;
import com.wyt.until.CommonResult;
import com.wyt.until.TableLayui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 采购信息
 */
@RestController
@RequestMapping("purchase")
public class PurchaseController {
    @Autowired
    public PurchaseMapper1 purchaseMapper; //采购
    @Autowired
    public BookMapper1 bookMapper;   //图书表格
    @Autowired
    public PurchaseDetailMapper1 purchaseDetailMapper;

    @Autowired
    public BookTypeMapper1 bookTypeMapper;
    @Autowired
    public HistoryOrderMapper1 historyOrderMapper;
    /**
     *    功能是审批进度，查询表格t_purchase中的相应的数据
     * @param page
     * @param limit
     * @param beginTime
     * @param endTime
     * @return
     */
    @RequestMapping("selectAllPurchaseByUid")
    public TableLayui selectAllPurchaseByUid(Integer page, Integer limit, String beginTime, String endTime){
        PageHelper.startPage(page,limit);
        List<Purchase1> purchases = purchaseMapper.findAllPurchase(beginTime,endTime);
        PageInfo<Purchase1> pageInfo = new PageInfo<>(purchases);
        return new TableLayui(0,"",pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 功能是采购信息
     * @param page
     * @param limit
     * @param typeId
     * @param bookName
     * @param bookPublisher
     * @return
     */
    @RequestMapping("selectAllBook")
    public TableLayui selectAllBook(Integer page,Integer limit,Integer typeId,String bookName,String bookPublisher){
        PageHelper.startPage(page,limit);
        String booktype=null;
        if(typeId!=null) {
            BookType1 bookType = bookTypeMapper.selectByPrimaryKey(typeId);
            booktype = bookType.getBooktype();
        }
        List<Book1> books = bookMapper.findThree(bookName,bookPublisher,booktype);
        PageInfo<Book1> pageInfo = new PageInfo<>(books);
        return new TableLayui(0,"",pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 审核信息
     * @param page
     * @param limit
     * @param pname
     * @param ename
     * @param beginTime
     * @param endTime
     * @return
     */
    @RequestMapping("selectAllPurchaseCheck")
    public TableLayui selectAllPurchaseCheck(Integer page,Integer limit,String pname,String ename,String beginTime,String endTime){
        PageHelper.startPage(page,limit);
        List<Purchase1> purchases = purchaseMapper.selectAllPurchaseCheck(pname,ename,beginTime,endTime);
        PageInfo<Purchase1> pageInfo = new PageInfo<>(purchases);
        return new TableLayui(0,"",pageInfo.getTotal(),pageInfo.getList());
    }

    /** 得到所有的驳回信息
     *
     * @param page
     * @param limit
     * @param ename
     * @param beginTime
     * @param endTime
     * @return
     */
    @RequestMapping("selectAllReject")
    public TableLayui selectAllReject(Integer page,Integer limit,String ename,String beginTime,String endTime){
        PageHelper.startPage(page,limit);
        List<Purchase1> purchases = purchaseMapper.selectAllReject(ename,beginTime,endTime);
        PageInfo<Purchase1> pageInfo = new PageInfo<>(purchases);
        return new TableLayui(0,"",pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 得到查询书籍类型
     * @return
     */
    @RequestMapping("getAllBookType")
    public List<BookType1> getAllBookType(){
        List<BookType1> allBookType = bookTypeMapper.findAllBookType();
        return allBookType;
    }
    /**采购信息中的提交审核
     *
     * @param bookList    前端传进来的数组 Book的类型
     * @param totalPrice
     * @param totalCount
     * @return
     */

    @RequestMapping("addPurchase")
    public CommonResult addPurchase(String bookList, String totalPrice, Integer totalCount, HttpSession session){

        List<Map<String,Object>> list=null;
        try {
            list = new ObjectMapper().readValue(bookList, List.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        Employee employee = (Employee) session.getAttribute("login");
        Purchase1 purchase = new Purchase1();
        purchase.setUid(employee.getdId());
        purchase.setPname(employee.geteName());
        purchase.setPdate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        purchase.setTotalCount(totalCount);
        purchase.setTotalPrice(totalPrice);
        //把申请人的 相应信息 插入到表格t_purchase
        purchaseMapper.Pinsert(purchase);
        Integer pid = purchase.getPid();

        for (Map<String,Object> map: list) {

            PurchaseDetail1 purchaseDetail = new PurchaseDetail1();
            purchaseDetail.setId(null);
            purchaseDetail.setBookid((Integer) map.get("bookid"));
            purchaseDetail.setBookname(map.get("bookname").toString());
            purchaseDetail.setAuthor(map.get("bookauthor").toString());
            purchaseDetail.setPublisher(map.get("bookpublisher").toString());
            purchaseDetail.setCount(Integer.parseInt( map.get("bookcount").toString()));
            purchaseDetail.setPrice(map.get("bookprice").toString());
            purchaseDetail.setTypeid((Integer) (map.get("typeid")));
//            purchaseDetail.setTypeid((Integer) (HashMap)map.get("booktype"));
            purchaseDetail.setPid(pid);
            purchaseDetailMapper.insert(purchaseDetail);
            System.out.println("========================================");
            System.out.println(map.get("booktype"));
        }

        return new CommonResult(200,"",null);
    }
    @RequestMapping("delReject")
    public CommonResult delReject(Integer pid,String bookList, String totalPrice, Integer totalCount, HttpSession session){

        List<Map<String,Object>> list=null;
        try {
            list = new ObjectMapper().readValue(bookList, List.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        purchaseMapper.deleteByPrimaryKey(pid);
        purchaseDetailMapper.deleteByPid(pid);

        Employee employee = (Employee) session.getAttribute("login");
        Purchase1 purchase = new Purchase1();
        purchase.setUid(employee.getdId());
        purchase.setPname(employee.geteName());
        purchase.setPdate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        purchase.setTotalCount(totalCount);
        purchase.setTotalPrice(totalPrice);
        purchase.setPid(pid);
        purchase.setStatus(0);

        purchaseMapper.insertSelective(purchase);
        //把申请人的 相应信息 插入到表格t_purchase
//        purchaseMapper.Pinsert(purchase);
//        Integer pid = purchase.getPid();
        System.out.println(bookList);
        for (Map<String,Object> map: list) {

            PurchaseDetail1 purchaseDetail = new PurchaseDetail1();

            purchaseDetail.setId(null);
            purchaseDetail.setBookid((Integer) map.get("bookid"));
            purchaseDetail.setBookname(map.get("bookname").toString());
            purchaseDetail.setAuthor(map.get("bookauthor").toString());
            purchaseDetail.setPublisher(map.get("bookpublisher").toString());
//            purchaseDetail.setCount(((Integer)map.get("bookcount")));
            purchaseDetail.setCount(Integer.parseInt(map.get("bookcount").toString()) );

            purchaseDetail.setPrice(map.get("bookmoney").toString());
            purchaseDetail.setTypeid((Integer) (map.get("typeid")));
//            purchaseDetail.setTypeid((Integer) (HashMap)map.get("booktype"));
            purchaseDetail.setPid(pid);
            purchaseDetailMapper.insert(purchaseDetail);
            System.out.println("========================================");
            System.out.println(purchaseDetail);
        }

        return new CommonResult(200,"",null);
    }

    /**
     *   得到采购细节中的信息  点击详情的信息
     * @param pid
     * @return
     */
    @RequestMapping("getpurchaseDetailMassage")
    public List<PurchaseDetail1> getpurchaseDetailMassage(Integer pid){

        List<PurchaseDetail1> purchaseDetails = purchaseDetailMapper.findAllByPid(pid);
        System.out.println(purchaseDetails);
        return purchaseDetails;
    }

    /**
     *
     * @param status  状态码设置2 设置  相应的审批人
     * @param pid   t_purchase
     * @return
     */
    @RequestMapping("updateStatusPass")
    public CommonResult updateStatusReject(Integer status,Integer pid,HttpSession session){
        Employee employee = (Employee) session.getAttribute("login");
        String name = employee.geteName();
        Integer id = employee.geteNumber();
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

        int row = purchaseMapper.updateStatusReject(status,pid,id,name,date);
         if(row>0){
             return new CommonResult(200,"操作成功",null);
         }else{
             return new CommonResult(500,"操作失败",null);
         }
    }
    @RequestMapping("updateStatusReject")
    public CommonResult updateStatusReject(Purchase1 purchase,HttpSession session){
        Employee employee = (Employee) session.getAttribute("login");
        String ename = employee.geteName();
        Integer eid = employee.geteNumber();
        String edate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        purchase.setEid(eid);
        purchase.
                setEdate(edate);
        purchase.setEname(ename);

        HistoryOrder1 historyOrder = new HistoryOrder1();
        historyOrder.setPid(purchase.getPid());
        historyOrder.setPname(purchase.getPname());
        historyOrder.setUid(purchase.getUid());
        historyOrder.setTotalPrice(purchase.getTotalPrice());
        historyOrder.setTotalCount(purchase.getTotalCount());
        historyOrder.setPdate(purchase.getPdate());
        historyOrder.setRejectReason(purchase.getRejectReason());
        historyOrder.setEid(purchase.getEid());
        historyOrder.setEname(purchase.getEname());
        historyOrder.setEdate(purchase.getEdate());

        int row1 = historyOrderMapper.insertSelective(historyOrder);

        int row = purchaseMapper.updateByPrimaryKeySelective(purchase);
        if(row>0 && row1>0) {
            return new CommonResult(200, "操作成功", null);
        }else{
            return new CommonResult(500,"操作失败",null);
        }
    }
    @RequestMapping("getpurchaseDetailBook")
    @ResponseBody
    public CommonResult selectMinBookCount(Integer pid){
        System.out.println(pid);
        List<PurchaseDetail1> p = purchaseDetailMapper.findAllByPid1(pid);
        System.out.println("=========================================================");
        return new CommonResult(200,"",p);
    }
//    @RequestMapping("selectAllBook")
//    public TableLayui selectAllBook(Integer page,Integer limit,Integer typeId,String bookName,String bookPublisher){
//        PageHelper.startPage(page,limit);
//
//        List<Book1> books = bookMapper.selectAllBook();
//        PageInfo<Book1> pageInfo = new PageInfo<>(books);
//        return new TableLayui(0,"",pageInfo.getTotal(),pageInfo.getList());
//    }
}
